// 监控相关类型定义

// 基础监控指标
export interface PerformanceMetrics {
  timestamp: string;
  cpu_usage: number;
  memory_usage: number;
  disk_usage: number;
  network_io: {
    bytes_sent: number;
    bytes_recv: number;
  };
  kafka_metrics: {
    broker_count: number;
    topic_count: number;
    partition_count: number;
    message_rate: number;
  };
}

// 系统指标
export interface SystemMetrics {
  timestamp: string;
  cpu: {
    usage_percent: number;
    load_average: number[];
  };
  memory: {
    total: number;
    available: number;
    used: number;
    usage_percent: number;
  };
  disk: {
    total: number;
    used: number;
    free: number;
    usage_percent: number;
  };
  network: {
    bytes_sent: number;
    bytes_recv: number;
    packets_sent: number;
    packets_recv: number;
  };
}

// 消费者组指标
export interface ConsumerGroupMetrics {
  timestamp: string;
  consumer_groups: {
    [groupId: string]: {
      group_id: string;
      state: string;
      members: number;
      topics: {
        [topicName: string]: {
          partitions: {
            [partitionId: number]: {
              current_offset: number;
              latest_offset: number;
              lag: number;
            };
          };
          total_lag: number;
          consumption_rate: number;
        };
      };
      total_lag: number;
      is_active: boolean;
    };
  };
  total_groups: number;
  active_groups: number;
  total_lag: number;
}

// 消费者组Lag历史
export interface ConsumerGroupLagHistory {
  group_id: string;
  topic_name?: string;
  lag_history: Array<{
    timestamp: string;
    group_id: string;
    topic_name: string;
    total_lag: number;
    consumption_rate: number;
    partitions: {
      [partitionId: number]: {
        current_offset: number;
        latest_offset: number;
        lag: number;
      };
    };
  }>;
  total_records: number;
}

// 吞吐量指标
export interface ThroughputMetrics {
  timestamp: string;
  topics: {
    [topicName: string]: {
      topic_name: string;
      partition_count: number;
      production_rate_per_minute: number;
      consumption_rate_per_minute: number;  // 新增消费速率
      estimated_bytes_per_minute: number;
      total_messages: number;               // 新增Topic总消息数
      backpressure_ratio: number;           // 新增背压比率
      partitions: {
        [partitionId: number]: {
          partition_id: number;
          leader: number;
          replicas: number[];
          isr: number[];
          high_water_mark: number;
          log_start_offset: number;
          message_count: number;
          is_under_replicated: boolean;
          timestamp: string;
        };
      };
    };
  };
  cluster_throughput: {
    total_production_rate_per_minute: number;  // 生产速率
    total_consumption_rate_per_minute: number; // 消费速率
    total_bytes_per_minute: number;
    total_messages_count: number;              // 集群总消息数量
    active_topics: number;
    total_partitions: number;
  };
}

// Topic吞吐量历史
export interface TopicThroughputHistory {
  topic_name: string;
  throughput_history: Array<{
    timestamp: string;
    topic_name: string;
    production_rate_per_minute: number;
    estimated_bytes_per_minute: number;
    partition_count: number;
  }>;
  total_records: number;
}

// 集群健康指标
export interface ClusterHealthMetrics {
  timestamp: string;
  cluster_id: string;
  broker_count: number;
  brokers: {
    [brokerId: string]: {
      broker_id: number;
      host: string;
      port: number;
      is_controller: boolean;
      is_available: boolean;
      rack?: string;
    };
  };
  overall_health: 'healthy' | 'warning' | 'critical';
  issues: string[];
  under_replicated_partitions: number;
}

// 集群健康历史
export interface ClusterHealthHistory {
  health_history: Array<{
    timestamp: string;
    cluster_id: string;
    broker_count: number;
    overall_health: string;
    under_replicated_partitions: number;
  }>;
  total_records: number;
}

// 监控摘要报告
export interface MonitoringSummaryReport {
  report_time: string;
  cluster_overview: {
    broker_count: number;
    topic_count: number;
    consumer_group_count: number;
    active_consumer_groups: number;
    overall_health: string;
  };
  performance_summary: {
    total_messages_per_minute: number;
    total_bytes_per_minute: number;
    total_consumer_lag: number;
    under_replicated_partitions: number;
  };
  top_topics: Array<{
    topic_name: string;
    production_rate: number;
    partition_count: number;
  }>;
  problematic_consumer_groups: Array<{
    group_id: string;
    total_lag: number;
    member_count: number;
    state: string;
  }>;
  alerts: string[];
}

// 分区指标
export interface PartitionMetrics {
  partition_id: number;
  leader: number;
  replicas: number[];
  isr: number[];
  high_water_mark: number;
  log_start_offset: number;
  message_count: number;
  is_under_replicated: boolean;
  timestamp: string;
}

// API响应类型
export interface ApiResponse<T> {
  success: boolean;
  data: T;
  message?: string;
  timestamp?: string;
}

// 时间范围选项
export type TimeRange = '1h' | '6h' | '24h' | '3d' | '7d';

// 健康状态
export type HealthStatus = 'healthy' | 'warning' | 'critical' | 'unknown';

// 消费者组状态
export type ConsumerGroupState = 'Stable' | 'PreparingRebalance' | 'CompletingRebalance' | 'Dead' | 'Empty';

// 图表数据点
export interface ChartDataPoint {
  time: string;
  value: number;
  [key: string]: any;
}

// 监控配置
export interface MonitoringConfig {
  refreshInterval: number;
  alertThresholds: {
    lagThreshold: number;
    cpuThreshold: number;
    memoryThreshold: number;
    diskThreshold: number;
  };
  retentionPeriod: {
    performance: number; // 小时
    lag: number; // 小时
    throughput: number; // 小时
    health: number; // 小时
  };
}

// Monitor页面专用类型
export interface ClusterInfo {
  broker_count: number
  controller_id: number
  cluster_id: string
  brokers: Array<{
    id: number
    host: string
    port: number
    rack: string | null
  }>
}

export interface MonitorOverview {
  cluster_info: ClusterInfo
  topic_count: number
  consumer_group_count: number
  total_partitions: number
  active_connections: number
  uptime: string
}

export interface TopicStats {
  name: string
  partition_count: number
  replication_factor: number
  latest_offset: number
  earliest_offset: number
}

export interface MonitorHealthStatus {
  status: string
  kafka_connection: boolean
  kafka_brokers: number
  timestamp: string
  uptime: string
  version: string
}

export interface MonitorPerformanceMetrics {
  timestamp: string
  cpu_usage: number
  memory_usage: number
  disk_usage: number
  network_io: {
    bytes_in: number
    bytes_out: number
  }
  kafka_metrics: {
    messages_per_second: number
    bytes_per_second: number
    active_connections: number
  }
}

export interface HistoricalDataPoint {
  timestamp: string
  cpu_usage: number
  memory_usage: number
  disk_usage: number
  network_bytes_in: number
  network_bytes_out: number
  kafka_messages_per_second: number
  kafka_bytes_per_second: number
  kafka_active_connections: number
}

export interface HistoricalData {
  timeRange: string
  dataPoints: HistoricalDataPoint[]
  summary: {
    cpu: { avg: number; max: number; min: number }
    memory: { avg: number; max: number; min: number }
    disk: { avg: number; max: number; min: number }
    data_points: number
    time_range: string
  }
}

export interface ChartData {
  systemUsage: any
  kafkaMetrics: any
  topicPartitions: any
}
